package com.android.memory;


import com.android.memory.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ToggleButton;

public class OptionsActivity extends Activity implements OnClickListener {
	 
	 public static final String PREFS_NAME = "MyPrefsFile";
	 final CharSequence[] items = {"Hard", "Medium", "Easy"};
	 
	 private ToggleButton musicButton;
	 private ToggleButton soundButton;
	 private Button diffButton;
	 
	 public static boolean musicPlayer=true;
	 public static boolean soundPlayer=true;
 
	 Handler optionsHandler = new Handler();
	 
	 @Override
		public boolean onCreateOptionsMenu(Menu menu) {
		    MenuInflater inflater = getMenuInflater();
		    inflater.inflate(R.menu.about, menu);
		    return true;
		} 
	 
	 // About pop up. 
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item) {
		   switch (item.getItemId()) {
		        case R.id.aboutMenu:     
		        	  // Create the alert box
		            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

		            // Set the message to display
		            alertbox.setMessage("This is a memory game made for the course DAT255 - Software Engineering Project at Chalmers university.\n \n" +
		            		"Created by: \n   Axel Pelling \n   Daniel Persson \n   Erik Bogren \n   Sebastian Pukki");
		             // show the alert box
		            alertbox.show();
		            break;
		   }
		   return true;
	 }
	 
	 
	 
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        //Fullscreen mode
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
	                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        
	        //Sets the layout
	        setContentView(R.layout.options);
	        
	        //Updates the views for the user specified options
	        optionsHandler.post(optionsRunnable);
	         
	        //Binds a listener to each button
	        musicButton = (ToggleButton) findViewById(R.id.musicTogglebutton);
	        musicButton.setOnClickListener(this);	
	        soundButton = (ToggleButton) findViewById(R.id.soundTogglebutton);
	        soundButton.setOnClickListener(this);
	        diffButton = (Button) findViewById(R.id.boardButton);
	        diffButton.setOnClickListener(this); 
	         	  
	 }

	 /* Option buttons
	  * Toggles between true/false
	  */
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.musicTogglebutton:
			if (musicButton.isChecked()) {
				musicPlayer = true;
				//Starts the music
			    Intent backgroundmusic= new Intent(this, MusicManagerService.class);
				startService(backgroundmusic);
			}
			else {
				musicPlayer = false;
				//Stops the music
			    Intent backgroundmusic= new Intent(this, MusicManagerService.class);
				stopService(backgroundmusic);
			}
			break;
			
		case R.id.soundTogglebutton:
			if (soundButton.isChecked())
				soundPlayer = true;
			else
				soundPlayer = false;
			break;
		case R.id.boardButton:
			
			// Alert dialog for difficulty
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Pick a difficulty");
			builder.setItems(items, new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int item) {
			    	if (items[item].equals("Hard"))
			    		GameActivity.setDifficulty(1);
			    					   
		            if (items[item].equals("Medium"))
		            	GameActivity.setDifficulty(2);
		            	
		            if (items[item].equals("Easy"))
		                GameActivity.setDifficulty(3);
		            
		            diffButton.setText(items[item]);
			    }
			});
			AlertDialog alert = builder.create();
			alert.show();
	        break;
		}
	}
	
	// Runnable for options
	private Runnable optionsRunnable = new Runnable() {
		public void run(){
			musicButton.setChecked(musicPlayer);
			soundButton.setChecked(soundPlayer);
			diffButton.setText(items[GameActivity.getDifficulty()-1]);
		}
	};
	
	
	@Override
    protected void onStop(){
       super.onStop();
		if(!MusicManagerService.getContinueMusic())
		{
	    	Intent backgroundmusic= new Intent(this, MusicManagerService.class);
			stopService(backgroundmusic);
		}

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putInt("difficulty",GameActivity.getDifficulty() );
      editor.putBoolean("music", musicPlayer);
      editor.putBoolean("sound", soundPlayer);
      
      // Commit the edits!
      editor.commit();
    }
	
	public void onStart() {
		super.onStart();
		//if music is on, then sets continueMusic to true
		if(OptionsActivity.musicPlayer)
			MusicManagerService.setContinueMusic(true);
	}
	
	public void onPause() {
		super.onPause();
		MusicManagerService.setContinueMusic(false);
	}
	
	public void onResume() {
		super.onResume();
		if(OptionsActivity.musicPlayer) {
			MusicManagerService.setContinueMusic(true);
	    	Intent backgroundmusic= new Intent(this, MusicManagerService.class);
			startService(backgroundmusic);
		}
	}
	
	public void onDestroy() {
		super.onDestroy();
	}

}
